package edu.unc.ils.memai.rw;

import java.io.BufferedReader;
import java.io.FileReader;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ResultsParser {

    static int COMBINATIONS = 24;
    static int WEIGHTS = 16;
    public static void main(String[] args) throws Exception
    {
        BufferedReader br = new BufferedReader(new FileReader("fulltext.out"));
        String line;
        List<String> paramsSet = new ArrayList<String>();
        List<List<Double>> valuesSet = new ArrayList<List<Double>>();
        String lastHeader = null;
        List<Double> currentValues = new ArrayList<Double>();
        while ((line = br.readLine()) != null)
        {
            if (line.startsWith("n="))
            {
                if (lastHeader != null) {
                    paramsSet.add(lastHeader);
                    valuesSet.add(currentValues);
                }
                lastHeader = line;
                currentValues = new ArrayList<Double>();
            }
            else
            {
                String[] pr = line.split(",");
                double r = Double.valueOf(pr[0]);
                double p = Double.valueOf(pr[1]);
                currentValues.add(p);
            }
        }
        

        
        for (int j=0; j<WEIGHTS; j++)
        {
            System.out.println("\n\n");
            String nrow = "n,";
            String krow = "k,";
            String btrow = "bt,";
            String ntrow = "nt,";
            String rtrow = "rt,";
            for (int k=0; k<COMBINATIONS; k++)
            {
                if (paramsSet.size() <= j+k*WEIGHTS)
                    break;
                String params = paramsSet.get(j+k*WEIGHTS);

                String[] nvpairs = params.split(", ");
                for (String nvpair: nvpairs)
                {
                    String[] fields = nvpair.split("=");
                    String n = fields[0];
                    String v = fields[1];
                    if (n.equals("n"))
                        nrow += v + ",";
                    else if (n.equals("k"))
                        krow += v + ",";
                    else if (n.equals("bt"))
                        btrow += v + ",";
                    else if (n.equals("nt"))
                        ntrow += v + ",";
                    else if (n.equals("rt"))
                        rtrow += v + ",";                
                }
            }
            System.out.println(nrow);
            System.out.println(krow);
            System.out.println(btrow);
            System.out.println(ntrow);
            System.out.println(rtrow);
            
            DecimalFormat df = new DecimalFormat("#.##");
            DecimalFormat df2 = new DecimalFormat("#.######");
            int i=0;
            for (double r = 0; r < 1; r+=.1)
            {
                
                String row = df.format(r) + ",";
                for (int k=0; k<COMBINATIONS; k++)
                {
                    if (valuesSet.size() <= j+k*WEIGHTS)
                        break;
                    
                    List<Double> values = valuesSet.get(j+k*WEIGHTS);
                    Double p = values.get(i);
                    row += df2.format(p) + ",";
                }
                i++;
                System.out.println(row);
            }
        }
    }
}
